import turtle
from turtle import *
from random import randint

speed(0)
penup()
goto(-140, 140)


# 定义每只乌龟初始化的函数
def each_turtle(x, y, t_color):
    et = Turtle()
    et.color(t_color)
    et.shape('turtle')
    et.penup()
    et.goto(x, y)
    et.pendown()
    for _ in range(5):
        et.right(72)
    return et


# 赛跑函数
def run(k):
    # 画跑道
    for step in range(k):
        write(step+1, align='center')
        right(90)
        for num in range(8):
            penup()
            forward(10)
            pendown()
            forward(10)
        penup()
        backward(160)
        left(90)
        forward(20)
    # 画乌龟
    red = each_turtle(-140, 100, 'red')
    blue = each_turtle(-140, 70, 'blue')
    purple = each_turtle(-140, 40, 'purple')
    green = each_turtle(-140, 10, 'turquoise')

    ls = [[red, 'red', 0], [blue, 'blue', 0], [purple, 'purple', 0], [green, 'turquoise', 0]]
    while 1:  # 赛跑中
        win = []    # 有到终点的加进这个列表
        for i in range(4):
            r = randint(1, 5)
            ls[i][-1] += r
            ls[i][0].forward(r)
            if ls[i][-1] >= 80:
                win.append(str(i+1))
        if win:     # 出现获胜者了就不用再循环了，着手打印出胜者字样
            turtle.pu()
            turtle.setpos(0, 200)
            turtle.color('red')
            turtle.ht()
            if len(win) == 1:
                turtle.write('%s号获胜' % win[0], align='center', font=('微软雅黑', 25, 'bold'))
            else:
                turtle.write('%s号并列第一' % '号、'.join(win), align='center', font=('微软雅黑', 25, 'bold'))
            break


run(10)   # 参数为跑道长度
turtle.done()
